/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.servlets;

import com.vo.Biblioteca;
import com.vo.Libro;
import com.vo.TipoLibro;
import com.vo.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Mile
 */
@WebServlet(name = "BibliotecaServlet", urlPatterns = {"/BibliotecaServlet"})
public class BibliotecaServlet extends HttpServlet {
    
    private Usuario usuario;
    private TipoLibro tipoLibro;
    private Libro libro;
    private Calendar fechaRetiro;
    private Biblioteca biblo = new Biblioteca();

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet BibliotecaServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<table border='1'");
            out.println("<tr><td  colspan='2'><h1>Consulta de Prestamo:  " + "</h1></td></tr>");
            out.println("<tr><td>ID : " + "</td><td>"+ this.usuario.getId()+"</td></tr>");
            out.println("<tr><td>Usuario : " + "</td><td>"+ this.usuario.getNombre()+"</td></tr>");
            out.println("<tr><td>Libro : " + "</td><td>" +this.libro.getNombre()+"</td></tr>");
            out.println("<tr><td>Fecha Retiro : " + "</td><td>"+ request.getParameter("fecha") +"</td></tr>");
            out.println("<tr><td>Tipo Libro : " + "</td><td>"+ this.libro.getTipoLibro().getTipo()+"</td></tr>");
            out.println("<tr><td>Periocidad : " + "</td><td>"+ this.libro.getTipoLibro().getDescripcion()+"</td></tr>");
            out.println("<tr><td>Concepto : "+ "</td><td>"+ this.biblo.getDiasMora() +" días Transcurridos" +"</td></tr>");
            out.println("<tr><td>Tiene una multa por valor de : " + "</td><td>$ "+this.biblo.getMulta()+"</td></tr>");
            out.println("</table>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String fecha[]=request.getParameter("fecha").split("/");
        
        int año=Integer.parseInt(fecha[2]);
        int mes=Integer.parseInt(fecha[1]);
        int dia=Integer.parseInt(fecha[0]);
        this.fechaRetiro= Calendar.getInstance();
        this.fechaRetiro.set(año, mes, dia);
        int tipoLibro=Integer.parseInt(request.getParameter("tipoLibro"));
        switch(tipoLibro){
            case 1:
                this.tipoLibro= new TipoLibro("A","Diario",500);
                break;
            case 2 :
                this.tipoLibro= new TipoLibro("B","Semanal",1000);
                break;
            case 3:
                this.tipoLibro= new TipoLibro("C","Mensual",2000);
                break;
        }
        
        this.usuario= new Usuario(Integer.parseInt(request.getParameter("id")), request.getParameter("nombreEstudiante"));
        this.libro= new Libro(request.getParameter("nombreLibro"), this.fechaRetiro, this.tipoLibro);
        
        long tiempoRetraso=this.biblo.calcularTiempoRetraso(año, mes, dia, this.libro);
        this.biblo.calcularMulta(this.libro, tiempoRetraso);
        processRequest(request, response);
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    
    
}
